<?php

namespace App\Http\Controllers;

use App\Exceptions\WechatException;
use App\Member;
use App\Models\ComboExchange;
use App\Repositories\ExchangeRepository;
use Illuminate\Http\Request;

class ExchangeController extends Controller
{
    public function index(){//兑换奖品页面

         return view('web.exchange.index');
    }
    public function post(Request $request,ExchangeRepository $exchangeRepository){//兑换码提交页面
        $member=Member::find(\Auth::guard('member')->id());
        if(!$request->code){
           throw  new WechatException('请输入兑换码');
        }
       $result= \Validator::make($request->all(),[
            'code'=>'size:9'
        ],[
            'code.size'=>'兑换码不正确'
       ]);

        if ($result->fails()){

            throw new WechatException($result->errors()->first());
        }
      $combo=  $exchangeRepository->ExchangeToUser($member,$request->code);
        return \Response::json([
            'code'=>'success',
            'msg'=>'您已经成功兑换'.$combo->title.'点击确定前往',
            'url'=>route('shake').'?time='.time()
        ]);
    }
}
